<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="实验室" prop="labCategory">
        <!-- <el-input v-model="queryParams.labId" placeholder="请输入实验室id" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.labCategory" placeholder="请选择实验室类别" clearable @change="handleQuery">
          <!-- <el-option :label="item.labCategory" :value="item.labCategory" v-for="(item,index) in laboratoryList" :key="index" /> -->
          <el-option label="软件实验室" value="1" />
          <el-option label="硬件实验室" value="2" />
          <el-option label="网络实验室" value="3" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="周次" prop="weekNumber">
        <el-input v-model="queryParams.weekNumber" placeholder="请输入周次" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="上课节次" prop="classPeriodId">
        <!-- <el-input v-model="queryParams.classPeriodId" placeholder="请输入第几节课" clearable
          @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.classPeriodId" placeholder="请选择上课节次" clearable @change="handleQuery">
          <el-option label="1-2" value="1" />
          <el-option label="3-5" value="2" />
          <el-option label="6-7" value="3" />
          <el-option label="8-9" value="4" />
          <el-option label="10-12" value="5" />
          <el-option label="13-15" value="6" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['labschedule:labschedule:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['labschedule:labschedule:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="experimentrequestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请唯一id" align="center" prop="requestId" />
      <el-table-column label="教师名" align="center" prop="teacherName" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="实验室类别" align="center" prop="labCategory">
        <template slot-scope="scope">
          {{ getCategoryName(scope.row.labCategory) }}
        </template>
      </el-table-column>
      <el-table-column label="班级id" align="center" prop="classId" />
      <el-table-column label="学生人数" align="center" prop="studentCount" />
      <el-table-column label="开始周次" align="center" prop="startWeek" />
      <el-table-column label="结束周次" align="center" prop="endWeek" />
      <el-table-column label="上课节次" align="center" prop="classPeriodId">
        <template slot-scope="scope">
          {{ getCourseNoName(scope.row.classPeriodId) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['lab:experimentrequest:edit']">排课</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['lab:experimentrequest:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改实验课安排对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="实验室" prop="labId">
          <el-select v-model="selectLabId" placeholder="请选择实验室">
            <el-option v-for="item in selectedValues" :key="item.labId" :label="item.labNumber"
              :value="item.labId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getValueSchedule, doSchedule, listLabschedule, getLabschedule, delLabschedule, addLabschedule, updateLabschedule } from "@/api/lab/labschedule";
import { listLaboratory } from "@/api/lab/laboratory";
import { Message } from "element-ui";

export default {
  name: "Labschedule",
  data() {
    return {
      // 实验室存储管理表格数据
      laboratoryList: [],
      requestId: 0,
      selectedValues: [],
      selectLabId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 实验课申请表格数据
      experimentrequestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        semesterId: null,
        labCategory: null,
        labId: null,
        weekNumber: null,
        classPeriodId: null,
        courseName: null,
        teacherId: null,
        className: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getAllList();
  },
  methods: {
    /** 查询实验室存储管理列表 */
    getAllList() {
      this.loading = true;
      listLaboratory(this.queryParams).then(response => {
        this.laboratoryList = response.rows;
        console.log(this.laboratoryList);
        // this.total = response.total;
        this.loading = false;
      });
    },
    getCategoryName(categoryId) {
      switch (categoryId) {
        case "1":
          return '软件实验室';
        case "2":
          return '硬件实验室';
        case "3":
          return '网络实验室';
        default:
          return '未知类别';
      }
    },
    getCourseNoName(categoryId) {
      switch (categoryId) {
        case 1:
          return '1-2';
        case 2:
          return '3-5';
        case 3:
          return '6-7';
        case 4:
          return '8-9';
        case 5:
          return '10-12';
        case 6:
          return '13-15';
        default:
          return '未知类别';
      }
    },
    getValueSchedule() {
      getValueSchedule()
    },
    /** 查询实验课安排列表 */
    getList() {
      this.loading = true;
      listLabschedule(this.queryParams).then(response => {
        // 实验课申请表格数据
        this.experimentrequestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.selectLabId = null
      this.selectedValues = []
      this.form = {
        scheduleId: null,
        semesterId: null,
        labId: null,
        weekNumber: null,
        classPeriodId: null,
        courseName: null,
        teacherId: null,
        className: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scheduleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实验课安排";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.requestId = row.requestId || this.ids
      const requestId = row.requestId || this.ids
      getValueSchedule(requestId).then(response => {
        this.selectedValues = response.data;
        console.log(this.form)
        this.open = true;
        this.title = "排课";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.selectLabId == null) {
        Message.warning("未选择")
        return
      }
      doSchedule(this.selectLabId, this.requestId).then(response => {
        this.$modal.msgSuccess("排课成功");
        this.open = false;
        this.getList();
      }).catch(err => {
        this.$modal.err("排课失败");
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scheduleIds = row.scheduleId || this.ids;
      this.$modal.confirm('是否确认删除实验课安排编号为"' + scheduleIds + '"的数据项？').then(function () {
        return delLabschedule(scheduleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('labschedule/labschedule/export', {
        ...this.queryParams
      }, `labschedule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
